package team.seekers.catering.user.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import team.seekers.catering.common.enums.EatingWishEnum;
import team.seekers.catering.common.result.ResultCode;
import team.seekers.catering.common.utils.CateringUtil;
import team.seekers.catering.food.service.ComboService;
import team.seekers.catering.user.entity.dto.update.OfflineUserUpdateDTO;
import team.seekers.catering.user.entity.dto.update.OnlineUserUpdateDTO;
import team.seekers.catering.user.entity.dto.update.UserShowInfoUpdateDTO;
import team.seekers.catering.common.result.R;
import team.seekers.catering.user.entity.vo.info.EatLogAnalyzeInfoVO;
import team.seekers.catering.user.entity.vo.info.UserInfoVO;
import team.seekers.catering.user.entity.vo.list.UserLogEatListVO;
import team.seekers.catering.user.service.BuildingService;
import team.seekers.catering.user.service.UserFitService;
import team.seekers.catering.user.service.UserLogEatService;
import team.seekers.catering.user.service.UserService;

import java.util.List;

/**
 * 前端控制器
 *
 * @author hbc
 * @since 2021-04-26
 */
@Api(description = "用户相关")
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private UserFitService userFitService;

    @Autowired
    private BuildingService buildingService;

    @Autowired
    private UserLogEatService userLogEatService;

    @Autowired
    private ComboService comboService;

    /**
     * 渲染个人信息（/api/user/get）
     */
    @ApiOperation(value = "渲染个人信息")
    @GetMapping("/get")
    public R get() {
        UserInfoVO userInfoVO = userService.get();
        return R.success().data("info", userInfoVO);
    }

    /**
     * 渲染个人身体数据信息
     */
    @ApiOperation(value = "渲染个人信息")
    @GetMapping("/fitInfo")
    public R fitInfo() {
        List<UserLogEatListVO> userLogEatList = userLogEatService.todayLogs();
        EatLogAnalyzeInfoVO analyzeInfoVO = new EatLogAnalyzeInfoVO();
        analyzeInfoVO.setEatLogVOList(userLogEatList);
        if (userLogEatList.size() != 0){
            analyzeInfoVO = comboService.setParamFromList(analyzeInfoVO);
        }
        analyzeInfoVO = userFitService.analyzeEatLog(analyzeInfoVO);
        return R.success().data("info",analyzeInfoVO);
    }

    /**
     * 修改名字和头像
     */
    @PutMapping("/updateShowInfo")
    public R updateShowInfo(@RequestBody UserShowInfoUpdateDTO updatePA) {
        updatePA.setId(CateringUtil.getUserId());
        userService.updateShowInfo(updatePA);
        return R.success();
    }

    /**
     * 填写身体数据成为线下正式用户（/api/user/bindOffline）
     */
    @PutMapping("/bindOffline")
    public R bindOffline(@RequestBody OfflineUserUpdateDTO offlineUserUpdateDTO) {
        if (EatingWishEnum.getByType(offlineUserUpdateDTO.getWish()) == null) {
            return R.fail("系统异常，请联系客服");
        }
        userFitService.bindOffline(offlineUserUpdateDTO);
        return R.success();
    }


    /**
     * 填写位置数据成为线上正式用户（/api/user/bindOnline）
     */
    @PutMapping("/bindOnline")
    public R bindOnline(@RequestBody OnlineUserUpdateDTO onlineUserUpdateDTO) {

        if (onlineUserUpdateDTO.getName().isEmpty()) {
            return R.fail("名字不能为空");
        }
        if (onlineUserUpdateDTO.getTel().isEmpty()) {
            return R.fail("电话不能为空");
        }
        String buildingId = buildingService.getByInfo(onlineUserUpdateDTO.getPark(), onlineUserUpdateDTO.getBuilding());
        if (buildingId == null) {
            return R.fail(ResultCode.BUILDING_NOT_FOUND);
        }

        onlineUserUpdateDTO.setBuildingId(buildingId);
        userService.bindOnline(onlineUserUpdateDTO);
        return R.success();
    }

//    /**
//     * 获取今天的饮食信息
//     */
//    @GetMapping("/todayEatAnalyze")
//    public R todayEatAnalyze(){
//        List<UserLogEatListVO> userLogEatList = userLogEatService.todayLogs();
//        EatLogAnalyzeInfoVO analyzeInfoVO = new EatLogAnalyzeInfoVO();
//        if (userLogEatList.size() != 0){
//            analyzeInfoVO.setEatLogVOList(userLogEatList);
//            analyzeInfoVO = comboService.setParamFromList(analyzeInfoVO);
//        }
//        return R.success().data("info",analyzeInfoVO);
//    }

//    /**
//     * 修改个人信息（/api/user/update）
//     */
//    @ApiOperation(value = "修改个人信息")
//    @PutMapping("/update")
//    public R update(@Valid @RequestBody OnlineUserUpdatePA onlineUserUpdatePA) {
//        userService.update(onlineUserUpdatePA);
//        return R.success();
//    }

}

